﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.General;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class MottoController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<MottoItem> _mottoRepository;
        private static MottoService _mottoService;

        public MottoController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _mottoRepository = new GenericRepository<MottoItem>(_context);

            _mottoService = new MottoService(_unitOfWork, _mottoRepository);
        }

        public ActionResult Index()
        {
            var itemList = _mottoService.GetAllElements().ToList();
            return View(itemList);
        }


        public ActionResult Details(int id)
        {
            return View();
        }


        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Edit(int id)
        {
            var item = _mottoService.GetElementById(id);
            return View(item);
        }


        [HttpPost]
        public ActionResult Edit(int id, MottoItem model)
        {
            try
            {
                // TODO: Add update logic here

                var item = _mottoService.GetElementById(id);
                model.CreatedDate = item.CreatedDate;
                model.ModifiedDate = item.ModifiedDate;
                _mottoService.UpdateElement(model);
                _mottoService.SaveChanges();

                return View(model);
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            return View();
        }


        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
